package cn.davidsu.library;

import ohos.agp.render.Shader;
import ohos.media.image.PixelMap;
import ohos.media.image.common.PixelFormat;
import ohos.media.image.common.Size;


/**
 * Created by cxzheng on 2018/4/29.
 * 阴影参数配置
 */

public class ShadowConfig {

    public static class Builder {
        //颜色
        private int mColor;
        //阴影颜色
        private int mShadowColor;
        //当需要渐变色时，可以传一个颜色数组进来，默认会从左到右渐进变色
        private int[] mGradientColorArray;
        //与上面的渐变色数组配合使用，指定每个颜色的位置（注意必须与上面的颜色数组length一样）
        private float[] mGradientPositions;
        //如果从左到右渐进变色的需求无法满足你，可以自定义一个LinearGradient传进来
        private Shader shader;
        //圆角半径
        private int mRadius;
        private int mShadowRadius;
        //阴影偏移量
        private int mOffsetX = 0;
        private int mOffsetY = 0;
        //shadow宽高
        private int width;
        private int height;


        public Builder() {
            //初始化默认值
            mColor = 0xff212121;
            mShadowColor = 0x4Dffffff;
            mRadius = 10;
            mShadowRadius = 16;
            mOffsetX = 0;
            mOffsetY = 0;
        }


        public Builder setColor(int color) {
            this.mColor = color;
            return this;
        }

        public Builder setShadowColor(int shadowColor) {
            this.mShadowColor = shadowColor;
            return this;
        }

        public Builder setGradientColorArray(int[] colorArray) {
            this.mGradientColorArray = colorArray;
            return this;
        }

        public Builder setGradientPositions(float[] positions) {
            this.mGradientPositions = positions;
            return this;
        }

        public Builder setShader(Shader shader) {
            this.shader = shader;
            return this;
        }

        public Builder setRadius(int radius) {
            this.mRadius = radius;
            return this;
        }

        public Builder setShadowRadius(int shadowRadius) {
            this.mShadowRadius = shadowRadius;
            return this;
        }

        public Builder setOffsetX(int offsetX) {
            this.mOffsetX = offsetX;
            return this;
        }

        public Builder setOffsetY(int offsetY) {
            this.mOffsetY = offsetY;
            return this;
        }

        public Builder setWidth(int width) {
            this.width = width;
            return this;
        }

        public Builder setHeight(int height) {
            this.height = height;
            return this;
        }

        /**
         * 根据颜色创建位图
         * @param color
         * @param width
         * @param height
         * @return
         */
        private PixelMap createPixelMap(int color, int width, int height){
            try {
                PixelMap.InitializationOptions options = new PixelMap.InitializationOptions();
                options.releaseSource = false;
                options.pixelFormat = PixelFormat.ARGB_8888;
                options.size = new Size(width, height);
                int[] colors = new int[width*height];
                for(int i=0;i<colors.length;i++)colors[i]=color;
                return PixelMap.create(colors,options);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }

        public CustomShadowBackground builder() {
            if (width==0||height==0)return null;
            PixelMap pixelMap = createPixelMap(0, width+mOffsetX, height+mOffsetY);
            return new CustomShadowBackground(mColor, mGradientColorArray, mGradientPositions, mShadowColor, shader, mRadius, mShadowRadius, mOffsetX, mOffsetY, pixelMap);
        }
    }
}
